$(function() {	
	//初始化BTN
	initBtnUI();
	
	//初始化窗口
	initWindowUI();
	
	//初始化数据表
	initDataGridUI();
	
});

//初始化BTN
function initBtnUI(){
	//任务节点事件BTN
	$('#addWorkflowTaskEventsBtn').linkbutton({plain:true,iconCls:'icon-add'});
	$('#editWorkflowTaskEventsBtn').linkbutton({plain:true,iconCls:'icon-edit'});
	$('#removeWorkflowTaskEventsBtn').linkbutton({plain:true,iconCls:'icon-remove'});
	$('#saveWorkflowTaskEventsBtn').linkbutton({plain:true,iconCls:'icon-save'});
	$('#closeWorkflowTaskEventsBtn').linkbutton({plain:true,iconCls:'icon-close'});
}


//初始化窗口
function initWindowUI(){
	//初始化任务节点事件窗口
	$('#workflowTaskEventWindow').window({
		title:'任务事件',
		closed:true,
		modal:true,
		closable:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		resizable:false,
		iconCls:'icon-task',
		width:700,
		height:400
	});
}

//初始化数据表
function initDataGridUI(){
	//初始化任务节点事件数据表格
	$('#workflowTaskEventsDataGrid').datagrid({
		url:'',
		idField:'id',
		sortName: 'id',
		sortOrder: 'desc',
		border:false,
		nowrap: false,
		striped: true,
		remoteSort:false,
		rownumbers:true,
		pagination:true,
		collapsible:true,
		singleSelect:true,
		toolbar:'#workflowTaskEventsToolBar',
		columns:[[
			{field:'orderIndex',title:'序号', width:80, editor:'text'},
			{field:'eventType',title:'事件类型', width:120, editor: {type:'combobox', options:{url:'workflowEventsAjax!loadWorkflowTaskEvetTypeData', valueField:'id', textField:'name', editable:false, required:true}}},
			{field:'executeStrategy',title:'执行策略', width:120, editor: {type:'combobox', options:{valueField:'id', textField:'name', data: [{"id":"CUSTOM_EVENT","name":"CUSTOM_EVENT"}], editable:false, required:true}}},
			{field:'serviceId',title:'执行业务Bean', width:100, editor:{type:'text', options:{required:true, messages:{required:'必须填写'}}}},
			{field:'serviceMethod',title:'执行业务方法', width:100, editor:'text'},
			{field:'methodParams',title:'方法参数', width:120, editor:'text'}
		]]
	});

	//分页
	var wtedgPager = $('#workflowTaskEventsDataGrid').datagrid('getPager');
	$(wtedgPager).pagination({
		pageSize: 15,
		pageList: [15, 30, 50, 100],
		beforePageText: '第',
		afterPageText: '页    共 {pages} 页',
		displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
		onSelectPage:function(pageNumber, pageSize){
			var row = $('#workflowTasksDataGrid').datagrid('getSelected');
			$('#workflowTaskEventsDataGrid').datagrid('options').url = 'workflowEventsAjax!loadWorkflowTaskEventsDataByTaskFunMount?id=' + row.id + '&pageNo=' + pageNumber + '&pageSize='+ pageSize;
			$('#workflowTaskEventsDataGrid').datagrid('reload', pageNumber);
		}
	});
}

//打开任务节点事件窗口
function openWorkflowTaskEventsWindow(){
	var row = $('#workflowTasksDataGrid').datagrid('getSelected');
	if(ROW_IS_NOT_NULL(row)){
		$('#workflowTaskEventsDataGrid').datagrid('options').url = 'workflowEventsAjax!loadWorkflowTaskEventsDataByTaskFunMount?id='+ +row.id+'&pageNo=1&pageSize=15';
		$('#workflowTaskEventsDataGrid').datagrid("options").pageNumber = 1;   
		$("#workflowTaskEventsDataGrid").datagrid('reload'); 
		$("#workflowTaskEventsDataGrid").datagrid('getPager').pagination({pageNumber: 1}); 
		$('#workflowTaskEventWindow').window('open');
	}
}

//新建任务节点事件
function addWorkflowTaskEvents(){	
	var row = $('#workflowTasksDataGrid').datagrid('getSelected');	
	$('#workflowTaskEventsDataGrid').datagrid('endEdit', lastIndex);
	$('#workflowTaskEventsDataGrid').datagrid('appendRow',{
		id:'',
		processDefinitionId:row.processDefinitionId,
		taskName:row.taskNodeName,
		orderIndex:'',
		eventScope:'TASK',
		eventType:'',
		executeStrategy:'',
		serviceId:'',
		serviceMethod:'',
		methodParams:'',
		workflowTaskFunMountId: row.id
	});	
	var lastIndex = $('#workflowTaskEventsDataGrid').datagrid('getRows').length-1;
	$('#workflowTaskEventsDataGrid').datagrid('selectRow', lastIndex);
	$('#workflowTaskEventsDataGrid').datagrid('beginEdit', lastIndex);
}

//编辑任务节点事件
function editWorkflowTaskEvents(){
	var row = $('#workflowTaskEventsDataGrid').datagrid('getSelected');
	if (ROW_IS_NOT_NULL(row)) {
		var rowIndex = $('#workflowTaskEventsDataGrid').datagrid('getRowIndex', row);
		$('#workflowTaskEventsDataGrid').datagrid('beginEdit', rowIndex);
	}
}

//删除任务节点事件
function removeWorkflowTaskEvents(){
	var row = $('#workflowTaskEventsDataGrid').datagrid('getSelected');
	if (row) {
		var rowIndex = $('#workflowTaskEventsDataGrid').datagrid('getRowIndex', row);
		$('#workflowTaskEventsDataGrid').datagrid('deleteRow', rowIndex);
	}
}

//保存任务节点事件
function saveWorkflowTaskEvents(){
		var rows = $('#workflowTaskEventsDataGrid').datagrid('getRows');
		for ( var i = 0; i < rows.length; i++) {
			$('#workflowTaskEventsDataGrid').datagrid('endEdit', i);
		}
		
		if ($('#workflowTaskEventsDataGrid').datagrid('getChanges').length) {
			var inserted = $('#workflowTaskEventsDataGrid').datagrid('getChanges', "inserted");
			var updated = $('#workflowTaskEventsDataGrid').datagrid('getChanges', "updated");
			var deleted = $('#workflowTaskEventsDataGrid').datagrid('getChanges', "deleted");
			var effectRow = new Object();
			
			if(inserted.length){
				effectRow["inserted"] = JSON.stringify(inserted);
			}
			
			if (updated.length) {
				effectRow["updated"] = JSON.stringify(updated);
			}
			
			if (deleted.length) {
				effectRow["deleted"] = JSON.stringify(deleted);
			}

			$.post("workflowEventsAjax!saveWorkflowEvents", effectRow,
					function(data) {
							$('#workflowTaskEventsDataGrid').datagrid('reload');
							$.messager.alert("提示", "保存成功!");					
			}, "json").error(function() {
				$('#workflowTaskEventsDataGrid').datagrid('reload');
				$.messager.alert("提示", "保存失败!");
			});
			
		}
}

//关闭任务节点事件窗口
function closeWorkflowTaskEventsWindow(){
	$('#workflowTaskEventWindow').window('close');
}
